package com.etc.service.impl;

import com.etc.dao.*;
import com.etc.entity.Doctor;
import com.etc.entity.Inhospital;
import com.etc.entity.Patient;
import com.etc.entity.Room;
import com.etc.service.InhospitalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @BelongsProject: HIS
 * @BelongsPackage: com.etc.service.impl
 * @Author:
 * @CreateTime: 2022-07-05  16:12
 * @Description: TODO
 * @Version: 1.0
 */

@Service
@Transactional
public class InhospitalserviceImpl implements InhospitalService {
    @Autowired
    private InhospitalMapper inhospitalMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Override
    public PageInfo<Inhospital> getInhospitalListByPage(String condition, int pageNum, int pageSize) {
        StringBuffer sb = new StringBuffer();
        //对参数进行判定并进一步处理
        if (condition != null && !"".equals(condition)) {
            sb.append("%").append(condition).append("%");
        }
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);
        // 执行Select查询语句
        List<Inhospital> byCondition = inhospitalMapper.find(sb.toString());
        // 封装分页对象
        PageInfo<Inhospital> pageInfo = new PageInfo<>(byCondition);
        return pageInfo;
    }

    @Override
    public int addInhospital(String patientName, String doctorName, String roomName, String inTime) {
        Room room = new Room();
        room.setRoomName(roomName);
        Doctor doctor = new Doctor();
        doctor.setDoctorName(doctorName);
        Patient patient = new Patient();
        patient.setPatientName(patientName);
        List<Room> roomList = roomMapper.selectiveFind(room);
        Inhospital inhospital = new Inhospital();
        for (Room room1 : roomList) {
            inhospital.setRoomId(room1.getRoomId());
            room.setTypeId(room1.getTypeId());
        }
        inhospital.setRoom(room);
        List<Doctor> doctorList = doctorMapper.findSelective(doctor);
        for (Doctor d : doctorList) {
            inhospital.setDoctorId(d.getDoctorId());
        }
        List<Patient> patientList = patientMapper.findSelective(patient);
        for (Patient p : patientList) {
            inhospital.setPatientId(p.getPatientId());
        }
        inhospital.setInTime(inTime);
        return inhospitalMapper.addInhospital(inhospital);
    }

    @Override
    public int updateInhospital(String inhospitalId,String patientName, String doctorName, String roomName,String inTime, String outTime) {
        Room room = new Room();
        room.setRoomName(roomName);
        Doctor doctor = new Doctor();
        doctor.setDoctorName(doctorName);
        Patient patient = new Patient();
        patient.setPatientName(patientName);
        List<Room> roomList = roomMapper.selectiveFind(room);
        Inhospital inhospital = new Inhospital();
        for (Room room1 : roomList) {
            inhospital.setRoomId(room1.getRoomId());
            room.setTypeId(room1.getTypeId());
        }
        inhospital.setRoom(room);
        List<Doctor> doctorList = doctorMapper.findSelective(doctor);
        for (Doctor d : doctorList) {
            inhospital.setDoctorId(d.getDoctorId());
        }
        List<Patient> patientList = patientMapper.findSelective(patient);
        for (Patient p : patientList) {
            inhospital.setPatientId(p.getPatientId());
        }
        inhospital.setInHospitalId(Integer.valueOf(inhospitalId));
        inhospital.setInTime(inTime);
        if(outTime!=null &&"".equals(outTime)) {
            inhospital.setOutTime(outTime);
        }else {
            inhospital.setOutTime(null);
        }
        return inhospitalMapper.updateInhospital(inhospital);
    }

    @Override
    public Inhospital getInhospital(String inhospitalId) {
        Inhospital inhospital =inhospitalMapper.findById(inhospitalId);
        return inhospital;
    }



}